package com.huarui.user.service;

import java.io.StringWriter;
import java.util.Date;
import java.util.Map;
import java.util.Properties;

import javax.mail.internet.ContentType;

import org.apache.log4j.Logger;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.exception.MethodInvocationException;
import org.apache.velocity.exception.ParseErrorException;
import org.apache.velocity.exception.ResourceNotFoundException;
import org.ewaf.framework.mail.Mail;
import org.ewaf.framework.mail.MailSender;
import org.ewaf.framework.mail.content.TextContent;
import org.ewaf.framework.struts2.velocity.VelocityUtil;
import org.ewaf.framework.util.MailUtil;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.web.context.WebApplicationContext;

/**
 * xzz
 * 
 * @author Administrator
 * 
 */
public class SendEmail implements ApplicationContextAware {

	private static final Logger log = Logger.getLogger(SendEmail.class);

	private Map env;

	public void setEnv(Map env) {
		this.env = env;
	}

	private String activeCode;

	public String getActiveCode() {
		return activeCode;
	}

	public void setActiveCode(String activeCode) {
		this.activeCode = activeCode;
	}

	private String userName;

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String email;

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	private MailSender mailSender;

	public void setMailSender(MailSender mailSender) {
		this.mailSender = mailSender;
	}

	public boolean send(Map<String, String> data) {

		try {

			VelocityContext ctx = new VelocityContext(data);
			ctx.put("env", env);

			String subject = mergeFile(ctx, data.get("subjectTemplate")
					.toString());
			String content = mergeFile(ctx, data.get("contentTemplate")
					.toString());

			Mail email = new Mail();
			email.setCharset(data.get("charset"));
			email.setSubject(subject);
			email.setGmtSent(new Date());
			email.setToAddress(MailUtil.str2Addr(data.get("recipients")));
			email.setContent(new TextContent(content, new ContentType(
					"text/html")));

			mailSender.send(email);
		} catch (Exception e) {
			log.error("failed to send mail to: " + data.get("recipients"), e);
			return false;
		}

		return true;
	}

	private String mergeFile(VelocityContext ctx, String fileName)
			throws ResourceNotFoundException, ParseErrorException, Exception,
			MethodInvocationException {
		Template template = null;
		VelocityEngine ve = new VelocityEngine();
		ve.setProperty(VelocityEngine.RUNTIME_LOG_LOGSYSTEM, this);
		ve.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH, context
				.getServletContext().getRealPath("/"));
		ve.init();
		template = ve.getTemplate(fileName, "utf-8");

		StringWriter sw = new StringWriter();

		template.merge(ctx, sw);

		String subject = sw.toString();
		return subject;
	}

	@Override
	public void setApplicationContext(ApplicationContext applicationContext)
			throws BeansException {
		// TODO Auto-generated method stub
		context = (WebApplicationContext) applicationContext;
	}

	private WebApplicationContext context;

}
